package _2022.contest.weekly.contest_291._6048_必须拿起的最小连续卡牌数;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： YHSimon
 * @date： 2022-05-01 11:22
 */
public class Solution {

    public int minimumCardPickup2(int[] cards) {
        int ans=Integer.MAX_VALUE;
        Map<Integer, List<Integer>> map=new HashMap<>();
        for(int i=0;i<cards.length;i++){
            if(map.containsKey(cards[i])){
                List<Integer> list = map.get(cards[i]);
                list.add(i);
                map.put(cards[i],list);
            }else{
                List<Integer> list=new ArrayList<>();
                list.add(i);
                map.put(cards[i],list);
            }
        }
        for(Map.Entry<Integer,List<Integer>> entry:map.entrySet()){
            if (entry.getValue().size()>=2){
                List<Integer> value = entry.getValue();
                for(int i=0;i<value.size();i++){
                    for(int j=i+1;j<value.size();j++){
                        ans=Math.min(ans, value.get(j)-value.get(i)+1);
                    }
                }
            }
        }
        return ans==Integer.MAX_VALUE?-1:ans;
    }


    public int minimumCardPickup(int[] cards) {
        int ans=Integer.MAX_VALUE;
        boolean flag=false;
        for(int i=0;i<cards.length;i++){
            for(int j = i+1; j<(flag ?i+ans-1:cards.length); j++){
                if (j<cards.length&&cards[i]==cards[j]){
                    ans=Math.min(ans,j-i+1);
                    flag=true;
                    break;
                }
            }
        }
        return ans==Integer.MAX_VALUE?-1:ans;
    }


    public static void main(String[] args) {
        Solution s = new Solution();
        // System.out.println(s.minimumCardPickup2(new int[]{3,4,2,3,4,7}));
        System.out.println(s.minimumCardPickup2(new int[]{1,0,5,3}));
    }
}
